﻿using System;
using System.Collections.Generic;
using System.Data;
using nkstdgia.common.ThamDinhGia.BusinessObjects;

namespace nkstdgia.common.ThamDinhGia.DataAccess
{
    public class GIA_THAM_DINHDA
    {
        #region ***** Init Methods *****

        public GIA_THAM_DINHDA()
        {
        }

        #endregion ***** Init Methods *****

        #region ***** Get Methods *****

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public GIA_THAM_DINH Populate(IDataReader myReader)
        {
            GIA_THAM_DINH obj = new GIA_THAM_DINH();
            obj.MA_HO_SO = (Int64)myReader["MA_HO_SO"];
            obj.GIA_GIAO_DICH_TRIEU_DONG = myReader["GIA_GIAO_DICH_TRIEU_DONG"] != DBNull.Value ? (double)myReader["GIA_GIAO_DICH_TRIEU_DONG"] : 0.0;
            obj.GIA_GIAO_DICH_CAY_VANG = myReader["GIA_GIAO_DICH_CAY_VANG"] != DBNull.Value ? (double)myReader["GIA_GIAO_DICH_CAY_VANG"] : 0.0;
            obj.THOI_DIEM_GIAO_DICH = myReader["THOI_DIEM_GIAO_DICH"] != DBNull.Value ? (DateTime)myReader["THOI_DIEM_GIAO_DICH"] : new DateTime();
            obj.LOAI_THANH_TOAN = (Int64)myReader["LOAI_THANH_TOAN"];
            obj.NOI_DUNG_THANH_TOAN = myReader["NOI_DUNG_THANH_TOAN"] != DBNull.Value ? (string)myReader["NOI_DUNG_THANH_TOAN"] : "";
            return obj;
        }

        /// <summary>
        /// Get GIA_THAM_DINH by ma_ho_so
        /// </summary>
        /// <param name="ma_ho_so">MA_HO_SO</param>
        /// <returns>GIA_THAM_DINH</returns>
        public GIA_THAM_DINH GetByMA_HO_SO(Int64 ma_ho_so)
        {
            using (IDataReader reader = SqlHelper.ExecuteReader(Data.ConnectionString, CommandType.StoredProcedure, "sproc_GIA_THAM_DINH_GetByMA_HO_SO", Data.CreateParameter("MA_HO_SO", ma_ho_so)))
            {
                if (reader.Read())
                {
                    return Populate(reader);
                }
                return null;
            }
        }

        /// <summary>
        /// Get all of GIA_THAM_DINH
        /// </summary>
        /// <returns>List<<GIA_THAM_DINH>></returns>
        public List<GIA_THAM_DINH> GetList()
        {
            using (IDataReader reader = SqlHelper.ExecuteReader(Data.ConnectionString, CommandType.StoredProcedure, "sproc_GIA_THAM_DINH_Get"))
            {
                List<GIA_THAM_DINH> list = new List<GIA_THAM_DINH>();
                while (reader.Read())
                {
                    list.Add(Populate(reader));
                }
                return list;
            }
        }

        /// <summary>
        /// Get DataSet of GIA_THAM_DINH
        /// </summary>
        /// <returns>DataSet</returns>
        public DataSet GetDataSet()
        {
            return SqlHelper.ExecuteDataSet(Data.ConnectionString, CommandType.StoredProcedure, "sproc_GIA_THAM_DINH_Get");
        }

        /// <summary>
        /// Get all of GIA_THAM_DINH paged
        /// </summary>
        /// <param name="recperpage">record per page</param>
        /// <param name="pageindex">page index</param>
        /// <returns>List<<GIA_THAM_DINH>></returns>
        public List<GIA_THAM_DINH> GetListPaged(int recperpage, int pageindex)
        {
            using (IDataReader reader = SqlHelper.ExecuteReader(Data.ConnectionString, CommandType.StoredProcedure, "sproc_GIA_THAM_DINH_GetPaged"
                            , Data.CreateParameter("recperpage", recperpage)
                            , Data.CreateParameter("pageindex", pageindex)))
            {
                List<GIA_THAM_DINH> list = new List<GIA_THAM_DINH>();
                while (reader.Read())
                {
                    list.Add(Populate(reader));
                }
                return list;
            }
        }

        /// <summary>
        /// Get DataSet of GIA_THAM_DINH paged
        /// </summary>
        /// <param name="recperpage">record per page</param>
        /// <param name="pageindex">page index</param>
        /// <returns>DataSet</returns>
        public DataSet GetDataSetPaged(int recperpage, int pageindex)
        {
            return SqlHelper.ExecuteDataSet(Data.ConnectionString, CommandType.StoredProcedure, "sproc_GIA_THAM_DINH_GetPaged"
                            , Data.CreateParameter("recperpage", recperpage)
                            , Data.CreateParameter("pageindex", pageindex));
        }

        #endregion ***** Get Methods *****

        #region ***** Add Update Delete Methods *****

        /// <summary>
        /// Add a new GIA_THAM_DINH within GIA_THAM_DINH database table
        /// </summary>
        /// <param name="obj">GIA_THAM_DINH</param>
        /// <returns>key of table</returns>
        public long Add(GIA_THAM_DINH obj)
        {
            SqlHelper.ExecuteNonQuery(Data.ConnectionString, CommandType.StoredProcedure, "sproc_GIA_THAM_DINH_Add"
                            , Data.CreateParameter("MA_HO_SO", obj.MA_HO_SO)
                            , Data.CreateParameter("GIA_GIAO_DICH_TRIEU_DONG", obj.GIA_GIAO_DICH_TRIEU_DONG)
                            , Data.CreateParameter("GIA_GIAO_DICH_CAY_VANG", obj.GIA_GIAO_DICH_CAY_VANG)
                            , Data.CreateParameter("THOI_DIEM_GIAO_DICH", obj.THOI_DIEM_GIAO_DICH)
                            , Data.CreateParameter("LOAI_THANH_TOAN", obj.LOAI_THANH_TOAN)
                            , Data.CreateParameter("NOI_DUNG_THANH_TOAN", obj.NOI_DUNG_THANH_TOAN)
            );
            return obj.MA_HO_SO;
        }

        /// <summary>
        /// updates the specified GIA_THAM_DINH
        /// </summary>
        /// <param name="obj">GIA_THAM_DINH</param>
        /// <returns></returns>
        public void Update(GIA_THAM_DINH obj)
        {
            SqlHelper.ExecuteNonQuery(Data.ConnectionString, CommandType.StoredProcedure, "sproc_GIA_THAM_DINH_Update"
                            , Data.CreateParameter("MA_HO_SO", obj.MA_HO_SO)
                            , Data.CreateParameter("GIA_GIAO_DICH_TRIEU_DONG", obj.GIA_GIAO_DICH_TRIEU_DONG)
                            , Data.CreateParameter("GIA_GIAO_DICH_CAY_VANG", obj.GIA_GIAO_DICH_CAY_VANG)
                            , Data.CreateParameter("THOI_DIEM_GIAO_DICH", obj.THOI_DIEM_GIAO_DICH)
                            , Data.CreateParameter("LOAI_THANH_TOAN", obj.LOAI_THANH_TOAN)
                            , Data.CreateParameter("NOI_DUNG_THANH_TOAN", obj.NOI_DUNG_THANH_TOAN)
            );
        }

        /// <summary>
        /// Delete the specified GIA_THAM_DINH
        /// </summary>
        /// <param name="ma_ho_so">MA_HO_SO</param>
        /// <returns></returns>
        public void Delete(Int64 ma_ho_so)
        {
            SqlHelper.ExecuteNonQuery(Data.ConnectionString, CommandType.StoredProcedure, "sproc_GIA_THAM_DINH_Delete", Data.CreateParameter("MA_HO_SO", ma_ho_so));
        }

        #endregion ***** Add Update Delete Methods *****
    }
}